package com.jic.order.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.order.entity.OrderReturnGoodRecord;
import com.jic.order.manage.request.OrderReturnGoodRecordRequest;
import com.jic.order.manage.response.OrderReturnGoodRecordResponse;
import com.jic.order.mapper.OrderReturnGoodRecordMapper;
import com.jic.order.service.OrderReturnGoodRecordService;
import com.jic.product.after.api.ProductListApi;
import com.jic.product.after.response.ProductAfterInfoByIdResponse;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单退货记录
 * @author : 
 * @email : 
 * @since : 2020-05-12 12:33:15
 * @version : v1.0.0
 */
@Service
@Slf4j
public class OrderReturnGoodRecordServiceImpl implements OrderReturnGoodRecordService {

    @Autowired
    private OrderReturnGoodRecordMapper orderReturnGoodRecordMapper;
    @Autowired
    private ProductListApi productListApi;

    @Override
    public long insert(OrderReturnGoodRecord orderReturnGoodRecord) {
        long flag = orderReturnGoodRecordMapper.insert(orderReturnGoodRecord);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return orderReturnGoodRecordMapper.delete(id);
    }

    @Override
    public long update(OrderReturnGoodRecord orderReturnGoodRecord) {
        return orderReturnGoodRecordMapper.update(orderReturnGoodRecord);
    }

    @Override
    public OrderReturnGoodRecord queryByPrimaryKey(Long id){
        return orderReturnGoodRecordMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<OrderReturnGoodRecord> query(OrderReturnGoodRecord orderReturnGoodRecord){
        return orderReturnGoodRecordMapper.query(orderReturnGoodRecord);
    }

    @Override
    public PageResult<OrderReturnGoodRecordResponse> queryPage(OrderReturnGoodRecordRequest orderReturnGoodRecordRequest, Page pageQuery) {
        // 计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<OrderReturnGoodRecordResponse> list = orderReturnGoodRecordMapper.queryPage(startIndex, pageQuery.getLimit(), orderReturnGoodRecordRequest);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(item -> {
                if (null != item.getGoodsId()) {
                    RestResult<ProductAfterInfoByIdResponse> responseRestResult = productListApi.queryProductInfoById(item.getGoodsId());
                    if (responseRestResult != null && responseRestResult.getData() != null && StringUtils.isNotEmpty(responseRestResult.getData().getChineseName())) {
                        item.setGoodsName(responseRestResult.getData().getChineseName());
                    }
                }
            });
        }
        long count = orderReturnGoodRecordMapper.queryPageCount(orderReturnGoodRecordRequest);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }
}